﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WebFunctions;
using HtmlAgilityPack;
using HtmlDoc = HtmlAgilityPack.HtmlDocument;

namespace AutoIndustryFiles
{
    class advantechScraper:FileLinksScraper
    {
        public advantechScraper()
        {
            this.FileLevel = 5;
            this.MainPage = "http://www.advantech.com.cn";
            this.CompanyName = "研华";
            this.CompanyUrl = "http://www.advantech.com.cn";
            this.ColumnName = "首页->产品";
            //this.ReviseXPath += new OnReviseXPath(FuJiScraper_ReviseXPath);

            WebLink wl = new WebLink();
            wl.LinkLevel = 0;
            wl.LinkId = 0;
            wl.LinkUrl = CompanyUrl;            
            WebLinks.Add(wl);
        }

        protected bool UpdateAndCancelled(string msg)
        {
            bool bCancelled = false;
            UpdateMsg("FujiScraper", msg, ref bCancelled);

            Application.DoEvents();

            return bCancelled;
        }

        public void GenerateCategory(WebLink wlParent)
        {
            string html = WebFuncs.OpenUrl(wlParent.LinkUrl);
            HtmlDoc doc = new HtmlDoc();
            GenerateNextLevelLinks2(doc, wlParent, @"//table[@class=""categoryContainer""]/");

            var links2 = from v1 in WebLinks
                        where v1.LinkLevel == 2
                        select new { link = v1 };

            foreach (var item in links2)
            {
                html = WebFuncs.OpenUrl(item.link.LinkUrl);
                doc.LoadHtml(html);
                GenerateNextLevelLinks2(doc, item.link, "xpath3");
            }

            var links3 = from v1 in WebLinks
                        where v1.LinkLevel == 3
                        select new { link = v1 };

            foreach (var item in links3)
            {
                html = WebFuncs.OpenUrl(item.link.LinkUrl);
                doc.LoadHtml(html);
                GenerateNextLevelLinks2(doc, item.link, "//ul[@class=\"listItem-product\"]/li/div[2]/h3/a");
            }

            var links4 = from v1 in WebLinks
                         where v1.LinkLevel == 4
                         select new { link = v1 };

            foreach (var item in links4)
            {
                //产品规格
                string url = @"http://support.advantech.com.cn/Support/SearchResult.aspx?keyword={0}&searchtabs=BIOS,Certificate,Datasheet,Driver,Firmware,Manual,Online%20Training,Software%20Utility,Utility,FAQ,Installation,Software%20API,Software%20API%20Manual&select_tab=Datasheet";
                url = string.Format(url, item.link.LinkName);
                html = WebFuncs.OpenUrl(item.link.LinkUrl);
                doc.LoadHtml(html);
                GenerateNextLevelLinks2(doc, item.link, "//ul[@class=\"listItem-product\"]/li/div[2]/h3/a");
                //使用手册
                url = @"http://support.advantech.com.cn/Support/SearchResult.aspx?keyword={0}&searchtabs=BIOS,Certificate,Datasheet,Driver,Firmware,Manual,Online%20Training,Software%20Utility,Utility,FAQ,Installation,Software%20API,Software%20API%20Manual&select_tab=Manual";
                url = string.Format(url, item.link.LinkName);
                html = WebFuncs.OpenUrl(item.link.LinkUrl);
                doc.LoadHtml(html);
                GenerateNextLevelLinks2(doc, item.link, "//ul[@class=\"listItem-product\"]/li/div[2]/h3/a");
            }
        }

        public override void GenerateAllLinks()
        {            
            string html = WebFuncs.OpenUrl("http://www.advantech.com.cn/products/");
            HtmlDoc doc = new HtmlDoc();
            GenerateSubWebLinks(doc, WebLinks[0], @"//div[@class=""DivAlign""][1]/table[1]"); //嵌入式主板&设计服务
            GenerateSubWebLinks(doc, WebLinks[0], @"//div[@class=""DivAlign""][1]/table[3]"); //工业自动化
            GenerateSubWebLinks(doc, WebLinks[0], @"//div[@class=""DivAlign""][2]/table[1]"); //应用计算机&嵌入式系统
            GenerateSubWebLinks(doc, WebLinks[0], @"//div[@class=""DivAlign""][2]/table[3]"); //医疗电脑
            GenerateSubWebLinks(doc, WebLinks[0], @"//div[@class=""DivAlign""][2]/table[5]"); //数字多媒体/服务器

            GenerateSubWebLinks(doc, WebLinks[1], @"//div[@class=""DivAlign""][1]/table[2]//span[@class=""text cateNameTitle""]"); //嵌入式主板&设计服务下级目录



            GenerateSubWebLinks(doc, WebLinks[2], @"//div[@class=""DivAlign""][1]/table[4]//span[@class=""text cateNameTitle""]"); //工业自动化
            GenerateSubWebLinks(doc, WebLinks[3], @"//div[@class=""DivAlign""][2]/table[2]//span[@class=""text cateNameTitle""]"); //应用计算机&嵌入式系统
            GenerateSubWebLinks(doc, WebLinks[4], @"//div[@class=""DivAlign""][2]/table[4]//span[@class=""text cateNameTitle""]"); //医疗电脑
            GenerateSubWebLinks(doc, WebLinks[5], @"//div[@class=""DivAlign""][2]/table[6]//span[@class=""text cateNameTitle""]"); //数字多媒体/服务器

            WebLink wl = new WebLink();
            wl.LinkLevel = 1;
            wl.LinkId = 1;
            wl.LinkUrl = CompanyUrl;
            wl.LinkName = "工业自动化";
            WebLinks.Add(wl);

            //工业自动化子目录
            GenerateCategory(wl);
        }
    }
}
